package com.rmy.queue.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.rmy.common.core.text.Convert;
import com.rmy.common.utils.DateUtils;
import com.rmy.common.utils.SecurityUtils;
import com.rmy.framework.manager.AsyncManager;
import com.rmy.queue.common.factory.DeviceAsyncFactory;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rmy.queue.mapper.QmsVoicConfigMapper;
import com.rmy.queue.domain.QmsVoicConfig;
import com.rmy.queue.service.IQmsVoicConfigService;
import rmy.VoicUtils;
import rmy.domain.VoicCmd;

/**
 * 语音配置Service业务层处理
 *
 * @author rmy
 * @date 2022-10-12
 */
@Service
public class QmsVoicConfigServiceImpl implements IQmsVoicConfigService
{
    @Autowired
    private QmsVoicConfigMapper qmsVoicConfigMapper;

    /**
     * 查询语音配置
     *
     * @param id 语音配置主键
     * @return 语音配置
     */
    @Override
    public QmsVoicConfig selectQmsVoicConfigById(Long id)
    {
        return qmsVoicConfigMapper.selectQmsVoicConfigById(id);
    }

    /**
     * 查询语音配置列表
     *
     * @param qmsVoicConfig 语音配置
     * @return 语音配置
     */
    @Override
    public List<QmsVoicConfig> selectQmsVoicConfigList(QmsVoicConfig qmsVoicConfig)
    {
        return qmsVoicConfigMapper.selectQmsVoicConfigList(qmsVoicConfig);
    }

    /**
     * 新增语音配置
     *
     * @param qmsVoicConfig 语音配置
     * @return 结果
     */
    @Override
    public int insertQmsVoicConfig(QmsVoicConfig qmsVoicConfig)
    {
        qmsVoicConfig.setCreateBy(SecurityUtils.getUsername());
        qmsVoicConfig.setCreateTime(DateUtils.getNowDate());
        return qmsVoicConfigMapper.insertQmsVoicConfig(qmsVoicConfig);
    }

    /**
     * 修改语音配置
     *
     * @param qmsVoicConfig 语音配置
     * @return 结果
     */
    @Override
    public int updateQmsVoicConfig(QmsVoicConfig qmsVoicConfig)
    {
        qmsVoicConfig.setUpdateTime(DateUtils.getNowDate());
        qmsVoicConfig.setUpdateBy(SecurityUtils.getUsername());
        return qmsVoicConfigMapper.updateQmsVoicConfig(qmsVoicConfig);
    }

    /**
     * 批量删除语音配置
     *
     * @param ids 需要删除的语音配置主键
     * @return 结果
     */
    @Override
    public int deleteQmsVoicConfigByIds(Long[] ids)
    {
        return qmsVoicConfigMapper.deleteQmsVoicConfigByIds(ids);
    }

    /**
     * 删除语音配置信息
     *
     * @param id 语音配置主键
     * @return 结果
     */
    @Override
    public int deleteQmsVoicConfigById(Long id)
    {
        return qmsVoicConfigMapper.deleteQmsVoicConfigById(id);
    }

    @Override
    public boolean sendVoicByCmd(String orgNo, String cmd, Map<String, Object> mapParams) {
        QmsVoicConfig qmsVoicConfig = new QmsVoicConfig();
        qmsVoicConfig.setOrgNo(orgNo);
        qmsVoicConfig.setStatus("0");// 启用
        List<QmsVoicConfig> qmsVoicConfigList = qmsVoicConfigMapper.selectQmsVoicConfigList(qmsVoicConfig);
        for (QmsVoicConfig tmpVoic : qmsVoicConfigList) {
            String strJson = tmpVoic.getContentTemplate();
            JSONObject jsonObject = JSONObject.parse(strJson);
            JSONArray jsonArray = jsonObject.getJSONArray("content");
            // 循环
            for (int i = 0; jsonArray!=null && i < jsonArray.size(); i++) {
                JSONObject jTmp = jsonArray.getJSONObject(i);
                VoicCmd voicCmd = VoicUtils.parseContent(jTmp.toString());
                if (!voicCmd.isDisable()) {// 启用
                    if (cmd.equalsIgnoreCase(voicCmd.getCmd())){//命令比对
                        String strContent = jTmp.toString();

                        for (Map.Entry<String, Object> entry : mapParams.entrySet()) {
                            strContent = strContent.replace(entry.getKey(), Convert.toStr(entry.getValue(), ""));
                        }
                        // 异步处理
                        AsyncManager.me().execute(
                                DeviceAsyncFactory.sendVoic(tmpVoic.getDeviceIp(),
                                        tmpVoic.getDevicePort().intValue(),
                                        tmpVoic.getJsonSettings(), strContent));
                    }
                }
            }

        }
        return true;
    }
}
